package Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DateBaseUpdateUtils {

	/*
	 * 根据姓名查IP
	 */
	public static String findUserIPAddress(String user) {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			Connection conn;
			conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			Statement stmt = conn.createStatement();

			ResultSet rs = stmt.executeQuery("SELECT IPAddress FROM user WHERE name = '" + user + "'");
			while (rs.next()) {
				// if (rs.getString(2).equals("")) {
				return rs.getString(1);
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;

	}

	/*
	 * 根据姓名和最新连接的IP更新数据库
	 */
	public static int updateUserIP(String user, String new_IP) {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			Connection conn;
			conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			Statement stmt = conn.createStatement();

			String sql = null;
			// UPDATE USER SET IPAddress = 'aa' WHERE NAME = 'cxy'
			sql = "UPDATE user SET IPAddress = '" + new_IP + "' WHERE NAME = '" + user + "'";
			int n = stmt.executeUpdate(sql);

			stmt.close();
			conn.close();
			return n;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
		}
		return 0;
	}

	/*
	 * 根据姓名，删除数据表中相应的IP记录值
	 */
	public static int deleteUserIP(String user) {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			Connection conn;
			conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			Statement stmt = conn.createStatement();

			String sql = null;
			// UPDATE USER SET IPAddress = 'aa' WHERE NAME = 'cxy'
			sql = "UPDATE user SET IPAddress = NULL WHERE NAME = '" + user + "'";
			int n = stmt.executeUpdate(sql);

			stmt.close();
			conn.close();
			return n;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
		}
		return 0;

	}

	/*
	 * 添加好友
	 */

	public static int addFriends(String mySelf, String friend) {

		ResultSet rs = null;
		Statement stmt = null;
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");

			conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			stmt = conn.createStatement();
			String sql = null;
			String result = null;

			rs = stmt.executeQuery("SELECT Friends FROM user WHERE name = '" + mySelf + "'");
			while (rs.next()) {
				result = rs.getString(1);
			}
			if (result != null) {
				sql = "UPDATE user SET Friends='" + result + "," + friend + "' WHERE NAME ='" + mySelf + "'";
			} else {
				sql = "UPDATE user SET Friends='" + friend + "' WHERE NAME ='" + mySelf + "'";

			}
			int n = stmt.executeUpdate(sql);
			return n;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return 0;

	}
}
